/**
 * @file autoLineWay.c
 * @author sevenfite (lin481399413@163.com)
 * @brief 自动线路规划接口文件
 * @version 0.1
 * @date 2024-08-31
 * 
 * @copyright Copyright (c) 2024
 * 
 */
#include "autoLineWay.h"
#include "plan.h"
#include "printf.h"
#include <stdint.h>
#include <stddef.h>
Edge gl_edges[] = {
  {1,2,1},
  {2,3,4},
  {3,4,1},
  {3,5,2},
  {2,5,2},
  {5,7,2},
  {5,12,2},
  {7,12,3},
  {5,6,1},
  {7,6,1},
  {6,14,2},
  {6,15,3},
  {12,6,3},
  {14,15,2},
  {15,37,2},
  {15,30,1},
  {30,50,2},
  {50,28,1},
  {28,25,2},
  {25,26,1},
  {26,27,4},
  {26,23,2},
  {23,24,4},
  {23,22,2},
  {22,27,2},
  {27,20,1},
  {20,18,2},
  {18,17,2},
  {20,17,3},
  {17,14,3},
};
/**
 * @brief 规划线路
 * 
 * @param nowNode 出发节点
 * @param targetNode 目标节点
 * @param targetNextNode 目标节点的下一个节点
 * @param way 存放规划的线路数组
 * @return int 
 */
int planLineWay(uint8_t nowNode , uint8_t targetNode , uint8_t targetNextNode , uint8_t * way){
  Graph* G = createGraph(gl_edges, sizeof(gl_edges) / sizeof(Edge));
  if(G == NULL){
    return 0;
  }
  int dist[MAX_VERTEXES];
  int path[MAX_VERTEXES];
  
  if(Dijkstra(G, dist, path, nowNode, targetNode)==false){
    deleteGraph(G);
    return 0;
  }
  getWay(path, targetNode, way, MAX_VERTEXES);
  deleteGraph(G);
  for (int i = 0; i < MAX_VERTEXES; i++) {
    if (way[i] == 0) {
      way[i] = targetNextNode;
      way[i+1] = 0;
      break;
    }
  }
  return 1;
}
void planLineWayTest(uint8_t nowNode , uint8_t targetNode , uint8_t targetNextNode){
    uint8_t way[MAX_VERTEXES];
    if(planLineWay(nowNode , targetNode , targetNextNode , way)){
        for (int i = 0; i < MAX_VERTEXES; i++) {
        if (way[i] == 0) {
            break;
        }
        printf("%d ", way[i]);
        }
        printf("\n");
    }else{
        printf("规划失败\n");
    }
}